package com.cxl.live.websocket.service;

import com.alibaba.fastjson.JSON;
import com.cxl.live.system.dto.UserInfo;
import com.cxl.live.websocket.model.Message;
import com.cxl.live.websocket.model.ResultMessage;
import com.cxl.live.websocket.server.RoomWebSocketServer;
import com.cxl.live.websocket.util.WebSocketUtil;

import javax.websocket.Session;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @Description: RoomService
 * @Author: ChenXueLi
 * @Date: 2019-02-05 15:20
 * 你是一段成熟的代码，要学会自己改bug
 */
public class RoomService {

    /**
     * 发送信息的方法
     * @param message
     */
    public void sendMessage(Message message) {
        ResultMessage resultMessage = message.getResultMessage();
        UserInfo userInfo = resultMessage.getUserInfo();

        Map<String, Object> map = new HashMap<>();
        map.put("content", message.getJsonData());
        map.put("name", userInfo.getInfoName());
        map.put("userId", userInfo.getFkUserId());
        resultMessage.setData(map);

        resultMessage.setFlag(true);

        // 获取房间中的所有用户session
        HashSet<Session> sessionHashSet = RoomWebSocketServer.getSessionListByRoom(message.getRoomUrl());
        // 发送数据给客户端
        WebSocketUtil.getInstance().sendMessage(sessionHashSet, JSON.toJSONString(resultMessage));
    }

}
